package com.achun.getcouponleague.presenter.impl;

import com.achun.getcouponleague.Bean.TiketBody;
import com.achun.getcouponleague.Bean.TiketContent;
import com.achun.getcouponleague.model.ServiceApi;
import com.achun.getcouponleague.presenter.ITiketPresenter;
import com.achun.getcouponleague.utils.Logutils;
import com.achun.getcouponleague.utils.RetrogitManager;
import com.achun.getcouponleague.view.ITiketCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TiketPresenter implements ITiketPresenter {
    private ITiketCallback iTiketCallback;
    private boolean isDataLoaded=false;
    private TiketBody tiketBody;
    private String url;
    private String title;
    private String cover;

    @Override
    public void registerCallback(ITiketCallback callback) {
        this.iTiketCallback=callback;
        //判断数据是否已经加载过了，如是则在加载
        if (isDataLoaded){
            createTiketCode(url,title,cover);
        }
    }

    @Override
    public void unRegisterCallback(ITiketCallback callback) {
if (iTiketCallback!=null){
    this.iTiketCallback=null;
}
    }

    @Override
    public void createTiketCode(String url,String title,String cover) {
        isDataLoaded=false;
        this.url=url;this.title=title;this.cover=cover;
        Retrofit retrofit = RetrogitManager.getInstance().getRetrofit();
        ServiceApi serviceApi = retrofit.create(ServiceApi.class);
        tiketBody = new TiketBody(url, null);
        Call<TiketContent> task = serviceApi.getTiketContent(tiketBody);
        task.enqueue(new Callback<TiketContent>() {
            @Override
            public void onResponse(Call<TiketContent> call, Response<TiketContent> response) {
                isDataLoaded=true;
                int code = response.code();
                if (code== HttpURLConnection.HTTP_OK){
                    TiketContent tiketContent = response.body();
                    Logutils.e(this.getClass(),"内容为--》"+tiketContent);
                    if (iTiketCallback!=null){
                        handleOnloaded(tiketContent,cover);
                    }
                }else {
                    //网络错误
                    if (iTiketCallback!=null){
                        handleOnError();
                    }
                }
            }

            @Override
            public void onFailure(Call<TiketContent> call, Throwable t) {
                isDataLoaded=true;
                //网络错误
                Logutils.e(this.getClass(),"网络错误"+t.toString());
                if (iTiketCallback!=null){
                    handleOnError();
                }
            }
        });


    }

    private void handleOnError() {
        iTiketCallback.onError();
    }

    private void handleOnloaded(TiketContent tiketContent,String cover) {
        try {
            String tiketcode = tiketContent.getData().getTbk_tpwd_create_response().getData().getModel();
            if (iTiketCallback!=null){
                iTiketCallback.onShowTiket(cover,tiketcode);
            }
        }catch (Exception e){
            if (iTiketCallback!=null){
                iTiketCallback.onEmpty();
            }
        }

    }
}
